package com.ijiuchuang.jc_localization.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.ijiuchuang.jc_localization.R;
import com.ijiuchuang.jc_localization.base.BaseActivity;
import com.ijiuchuang.jc_localization.base.FragmentController;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class HomeActivity extends BaseActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    @BindView(R.id.home_toolbar)
    Toolbar toolbar;
    @BindView(R.id.home_fab)
    FloatingActionButton fab;
    @BindView(R.id.nav_view)
    NavigationView navigationView;
    @BindView(R.id.drawer_layout)
    DrawerLayout drawer;
    @BindView(R.id.home_coordinator)
    CoordinatorLayout homeCoordinator;
    @BindView(R.id.home_app_bar)
    AppBarLayout appBar;

    private FragmentController controller;

    //记录Fragment的位置
    private int position = 0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        ButterKnife.bind(this);

        controller = FragmentController.getInstance(this, R.id.home_frame);
        controller.showFragment(0);

        // pink appBar折叠功能保留

        setSupportActionBar(toolbar);

        final ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        navigationView.setNavigationItemSelectedListener(this);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        position = savedInstanceState.getInt("position");
        controller.showFragment(position);
        super.onRestoreInstanceState(savedInstanceState);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        //记录当前的position
        outState.putInt("position", position);
    }

    @Override
    public void onBackPressed() {
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            Snackbar.make(homeCoordinator, R.string.snack_title_exit, Snackbar.LENGTH_SHORT)
                    .setCallback(new Snackbar.Callback() {
                        @Override
                        public void onDismissed(Snackbar snackbar, int event) {
                            super.onDismissed(snackbar, event);
                            // Snack消失的事件
                            /** Indicates that the Snackbar was dismissed from a new Snackbar being shown.*/
                            if (event == Snackbar.Callback.DISMISS_EVENT_CONSECUTIVE) {
                                finish();
                                System.exit(0);
                            }

                            fab.setEnabled(true);
                        }

                        @Override
                        public void onShown(Snackbar snackbar) {
                            super.onShown(snackbar);

                            fab.setEnabled(false);
                        }
                    })
                    .show();
        }

        return false;
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        FragmentController.onDestroy();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_home, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        switch (id) {
            case R.id.menu_home_location:
                //gotoActivity(LocationActivity.class);
                gotoActivity(PositionActivity.class);
                return true;
            case R.id.menu_home_tools:
                Intent intentTools = new Intent(this, FragmentActivity.class);
                intentTools.putExtra(FragmentActivity.FRAGMENT_NAME, FragmentActivity.FRAGMENT_NAME_TOOLS);
                startActivity(intentTools);
                return true;
            case R.id.menu_home_settings:
                Intent intentSetts = new Intent(this, FragmentActivity.class);
                intentSetts.putExtra(FragmentActivity.FRAGMENT_NAME, FragmentActivity.FRAGMENT_NAME_SETTINGS);
                startActivity(intentSetts);
                return true;
            case R.id.menu_home_feedback:
                return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        int id = item.getItemId();

        switch (id) {
            case R.id.nav_home:
                controller.showFragment(0);
                toolbar.setTitle(R.string.nav_home);
                break;
            case R.id.nav_location:
                gotoActivity(LocationActivity.class);
                break;
            case R.id.nav_whatshot:
                controller.showFragment(1);
                toolbar.setTitle(R.string.nav_whatshot);
                break;
            case R.id.nav_technology:
                controller.showFragment(2);
                toolbar.setTitle(R.string.nav_technology);
                break;
            case R.id.nav_tools:
                Intent intentTools = new Intent(this, FragmentActivity.class);
                intentTools.putExtra(FragmentActivity.FRAGMENT_NAME, FragmentActivity.FRAGMENT_NAME_TOOLS);
                startActivity(intentTools);
                break;
            case R.id.nav_settings:
                Intent intentSetts = new Intent(this, FragmentActivity.class);
                intentSetts.putExtra(FragmentActivity.FRAGMENT_NAME, FragmentActivity.FRAGMENT_NAME_SETTINGS);
                startActivity(intentSetts);
                break;
        }

        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

    @OnClick(R.id.home_fab)
    public void onClick() {
        Snackbar.make(homeCoordinator, R.string.snack_title_cooperation, Snackbar.LENGTH_LONG)
                .setAction(R.string.snack_action_ok, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // pink 商业合作
                    }
                }).show();
    }
}
